<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 检查登录状态
check_login();

// 初始化数据库连接
$db = db_connect();

// 获取公告类型映射
$notice_types = [
    1 => ['name' => '普通公告', 'badge' => 'badge-secondary'],
    2 => ['name' => '重要公告', 'badge' => 'badge-warning'],
    3 => ['name' => '紧急公告', 'badge' => 'badge-danger']
];

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = trim($_POST['title']);
    $content = trim($_POST['content']);
    $type = intval($_POST['type']);
    $status = isset($_POST['status']) ? 1 : 0;

    try {
        // 插入数据库
        $stmt = $db->prepare("INSERT INTO notices (title, content, type, status, created_at, updated_at) VALUES (:title, :content, :type, :status, NOW(), NOW())");
        $stmt->bindValue(':title', $title);
        $stmt->bindValue(':content', $content);
        $stmt->bindValue(':type', $type);
        $stmt->bindValue(':status', $status);
        
        if ($stmt->execute()) {
            redirect('announcements.php', '发布成功');
        } else {
            redirect('announcement_add.php', '发布失败，请重试。', 'error');
        }
    } catch (Exception $e) {
        redirect('announcement_add.php', '系统错误：' . $e->getMessage(), 'error');
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布公告 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
    .card {
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        padding: 25px;
    }
    .form-group {
        margin-bottom: 20px;
    }
    .form-control {
        width: 100%;
        padding: 8px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        transition: border-color 0.3s;
    }
    .form-control:focus {
        border-color: #3498db;
        outline: none;
    }
    textarea.form-control {
        min-height: 200px;
        resize: vertical;
    }
    .help-text {
        font-size: 12px;
        color: #666;
        margin-top: 5px;
    }
    .type-select {
        display: flex;
        gap: 15px;
        margin: 10px 0;
    }
    .type-option {
        padding: 8px 15px;
        border: 1px solid #ddd;
        border-radius: 4px;
        cursor: pointer;
        transition: all 0.3s;
    }
    .type-option:hover {
        border-color: #3498db;
    }
    .type-option.active {
        background: #3498db;
        color: #fff;
        border-color: #3498db;
    }
    </style>
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>发布公告</h2>
                </div>
                <div class="header-right">
                    <a href="announcements.php" class="btn btn-secondary">
                        <i class="fas fa-arrow-left"></i> 返回列表
                    </a>
                </div>
            </header>

            <div class="content">
                <div class="card">
                    <form action="" method="POST">
                        <div class="form-group">
                            <label for="title">公告标题</label>
                            <input type="text" id="title" name="title" class="form-control" required>
                        </div>

                        <div class="form-group">
                            <label>公告类型</label>
                            <div class="type-select">
                                <?php foreach ($notice_types as $key => $value): ?>
                                <label class="type-option">
                                    <input type="radio" name="type" value="<?php echo $key; ?>" 
                                           <?php echo $key == 1 ? 'checked' : ''; ?> style="display: none;">
                                    <span><?php echo $value['name']; ?></span>
                                </label>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="content">公告内容</label>
                            <textarea name="content" id="content" class="form-control" required></textarea>
                            <div class="help-text">支持换行，请注意排版</div>
                        </div>

                        <div class="form-group">
                            <label>
                                <input type="checkbox" name="status" value="1" checked>
                                立即发布
                            </label>
                        </div>

                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-paper-plane"></i> 发布公告
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
    // 公告类型选择效果
    document.querySelectorAll('.type-option').forEach(option => {
        option.addEventListener('click', function() {
            document.querySelectorAll('.type-option').forEach(opt => opt.classList.remove('active'));
            this.classList.add('active');
            this.querySelector('input').checked = true;
        });

        if (option.querySelector('input').checked) {
            option.classList.add('active');
        }
    });
    </script>
</body>
</html> 